package com.d3.pomodoro.fragment

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.d3.pomodoro.R
import com.d3.pomodoro.utils.FragmentUserVisibleController
import com.d3.pomodoro.utils.SoftKeyBoardController
import com.d3.pomodoro.utils.ThemeController
import com.yanzhenjie.recyclerview.SwipeRecyclerView


abstract class BaseFragment : Fragment(), FragmentUserVisibleController.UserVisibleCallback,
    View.OnClickListener {
    private lateinit var userVisibleController: FragmentUserVisibleController
    var mMainView: View? = null
    var themeController: ThemeController? = null
    var softKeyBoardChangeListener: SoftKeyBoardController? = null
    var baseRecycleView: SwipeRecyclerView? = null
    var toolBar: Toolbar? = null


    companion object {
        const val MAIN_PAGR_TAG = 1000
        const val MAIN_PAGE_BACK_STACK = "MAIN_PAGE_BACK_STACK"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        userVisibleController = FragmentUserVisibleController(this, this)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mMainView = View.inflate(activity, getFragmentLayoutId(), null)
        return mMainView
    }

    abstract fun getFragmentLayoutId(): Int
    abstract fun initThemeViews()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (activity != null) {
            softKeyBoardChangeListener = SoftKeyBoardController(activity!!)
            themeController = ThemeController(activity!!)
            initThemeViews()
            themeController?.initTheme()
        }
        initViews()
        initData()
    }

    open fun initViews() {
        //https@ //blog.csdn.net/yanzhenjie1003/article/details/52115566
        baseRecycleView = mMainView?.findViewById(R.id.baseRecycleView)
        toolBar = mMainView?.findViewById(R.id.toolBar)
    }

    open fun initData() {

    }

    open fun refreshToolBarTitle(title: String) {
        toolBar?.title = title
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        userVisibleController.activityCreated()
    }

    override fun onResume() {
        super.onResume()
        userVisibleController.resume()
    }

    override fun onPause() {
        super.onPause()
        userVisibleController.pause()
    }

    override fun onClick(v: View?) {

    }

    override var isWaitingShowToUser: Boolean
        get() = userVisibleController.isWaitingShowToUser()
        set(value) {
            userVisibleController.setWaitingShowToUser(value)
        }
    override val isVisibleToUser: Boolean
        get() = userVisibleController.isVisibleToUser()

    override fun callSuperSetUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
    }

    override fun onVisibleToUserChanged(isVisibleToUser: Boolean, invokeInResumeOrPause: Boolean) {
    }


    fun openFragment(fragment: Fragment, target: Int, backStack: String) {
        fragment.setTargetFragment(this, target)
        if (activity?.supportFragmentManager != null) {
            activity?.supportFragmentManager!!.beginTransaction()
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .add((view?.parent as ViewGroup).id, fragment)
                .hide(this)
                .show(fragment)
                .addToBackStack(backStack)
                .commit()
        }
    }

    fun closeFragment() {
        fragmentManager?.popBackStack()
    }

    fun closeFragmentForResult(resultCode: Int, intent: Intent) {
        targetFragment?.onActivityResult(targetRequestCode, resultCode, intent)
        closeFragment()
    }

    fun changeStatusBarColor(colorId: Int) {
        if (Build.VERSION.SDK_INT >= 21) {
            activity?.window?.statusBarColor = resources.getColor(colorId)
        }
    }

}